﻿using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace classTest
{
    /// <summary>
    /// IdentifyWindow.xaml 的交互逻辑
    /// </summary>
    public partial class IdentifyQueryWindow : Window
    {
        private List<Feature> pListFeatures = new List<Feature>();
        private List<FeatureLayer> pListFeaturesLayer = new List<FeatureLayer>();
        private Boolean first = true;
        private FeatureLayer fl;
        private MapPoint cPoint;
        private MapPoint mPoint;

        private List<Feature> FeatureQuery { set; get; }

        private MapView mapView { set; get; }
        public Boolean isLoad = true;
        public MapPoint MPoint
        {
            set
            {
                mPoint=value;
            }
            get {
                return mPoint;
            }
        }
        public IdentifyQueryWindow(MapView mapView1,MapPoint mPoint1, List<Feature> FeatureQuery1)
        {
            InitializeComponent();
            mPoint = mPoint1;
            mapView = mapView1;
            cPoint = mPoint;
            FeatureQuery = FeatureQuery1;
            Initialize();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            textBox.Text = $"{Math.Round(mPoint.X, 3)} {Math.Round(mPoint.Y, 3)} 米";
            first = false;
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            if (first) return;
            //Console.WriteLine($"{mPoint.X} {mPoint.Y}  -> {cPoint.X} {cPoint.Y}");
            if (mPoint!= cPoint)
            {
                cPoint = mPoint;
                textBox.Text = $"{Math.Round(mPoint.X, 3)}  {Math.Round(mPoint.Y, 3)} 米";
                IdentifyQuery();
            }
        }
        private void Window_Closed(object sender, EventArgs e)
        {
            try
            {
                if (fl != null)
                    fl.ClearSelection();
                if (FeatureQuery != null && FeatureQuery.Count > 0) FeatureQuery.Clear();
            }
            catch
            {

            }

            isLoad = false ;
        }

        private void Initialize()
        {
            IdentifyQuery();
        }

        private async void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox.SelectedIndex != -1)
            {
                Feature ft = pListFeatures[comboBox.SelectedIndex];//获取索引所在图层
                if (FeatureQuery != null && FeatureQuery.Count > 0) FeatureQuery.Clear();
                FeatureQuery.Add(ft);
                if (fl!=null)
                fl.ClearSelection();
                fl = pListFeaturesLayer[comboBox.SelectedIndex];

                string introduction = "暂无介绍";
                for (int i = 0; i < ft.Attributes.Keys.Count; i++)
                {
                    if (ft.Attributes.Keys.ElementAt(i).ToLower() == "introduction")
                    {
                        introduction = ft.Attributes.Values.ElementAt(i).ToString();
                        break;
                    }
                }
                Random random = new Random();
                var picPath = $"..\\..\\Image\\scauImage\\{random.Next(1, 39)}.jpg";
                pointImage.Source = new BitmapImage(new Uri(picPath, UriKind.Relative));
                Introduction.Text = introduction;

                dataGrid.AutoGenerateColumns = true;//数据格网对象自动生成列
                dataGrid.ItemsSource = ft.Attributes;//设置数据格网对象中项的数据源

                pListFeaturesLayer[comboBox.SelectedIndex].SelectFeature(ft);
                await mapView.SetViewpointGeometryAsync(pListFeatures[comboBox.SelectedIndex].Geometry.Extent, 50);
            }
        }

        private async void IdentifyQuery()
        {
            //创建查询条件
            pListFeatures.Clear();
            pListFeaturesLayer.Clear();
            comboBox.Items.Clear();

            IReadOnlyList<IdentifyLayerResult>  res = await mapView.IdentifyLayersAsync(mapView.LocationToScreen(mPoint),5,false);
            foreach(IdentifyLayerResult identifyLayerResult in res)
            {
                try
                {
                    IReadOnlyList<GeoElement> geoElements = identifyLayerResult.GeoElements;
                    pListFeatures.Add(geoElements.First() as Feature);
                    pListFeaturesLayer.Add(identifyLayerResult.LayerContent as FeatureLayer);
                }
                catch
                {

                }

            }
            couuntLayer.Content = $"识别了 {pListFeatures.Count} 个要素";

            for (int i = 0; i < pListFeatures.Count; i++)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = pListFeaturesLayer[i].Name;
                comboBox.Items.Add(cbi);
            }
            if(comboBox.Items.Count>0) comboBox.SelectedIndex = 0;
        }
    }
}
